# Contributor: Robert T. McGibbon <rmcgibbo@gmail.com>

_realname=netcdf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.3.3
pkgrel=1
pkgdesc="Interface for scientific data access to large binary data (mingw-w64)"
arch=('any')
license=('custom')
url="http://www.unidata.ucar.edu/software/netcdf/"
depends=("${MINGW_PACKAGE_PREFIX}-hdf5")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
			 "${MINGW_PACKAGE_PREFIX}-cmake")
source=("ftp://ftp.unidata.ucar.edu/pub/netcdf/${_realname}-${pkgver}.zip"
        "0001-ocinternal-mkdir-patch.patch"
        "0002-onstack.patch")
sha256sums=('77ad3164002b5a33de1247fcf3f8d2ee395781663261aae3e1d5b05822669dc6'
            'ee19e096e58fbd86f994c2367fbece92a54cf006873dadfe4edacdccb1471db0'
            'bb99fd1bca1fd67b1fbf2418efec7f09ff84a9797ba21c258e33a0cc717f0929')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/0001-ocinternal-mkdir-patch.patch
  patch -p1 -i "${srcdir}"/0002-onstack.patch
}

build() {
  [[ -d ${srcdir}/build-static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-static-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-static-${MINGW_CHOST} && cd ${srcdir}/build-static-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -G "MSYS Makefiles" \
	-DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
	-DCMAKE_BUILD_TYPE=Release \
	-DBUILD_SHARED_LIBS=OFF \
	-DENABLE_DLL=OFF \
	-DENABLE_DAP=OFF \
	-DBUILD_TESTING=OFF \
	-DENABLE_DYNAMIC_LOADING=OFF \
	-DENABLE_NETCDF_4=ON \
	"${srcdir}/${_realname}-${pkgver}"
  make || make

  [[ -d ${srcdir}/build-shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-shared-${MINGW_CHOST} && cd ${srcdir}/build-shared-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -G "MSYS Makefiles" \
	-DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
	-DCMAKE_BUILD_TYPE=Release \
	-DBUILD_SHARED_LIBS=ON \
	-DENABLE_DAP=OFF \
	-DBUILD_TESTING=OFF \
	-DENABLE_DYNAMIC_LOADING=OFF \
	-DENABLE_NETCDF_4=ON \
	"${srcdir}/${_realname}-${pkgver}"
  make || make
}

package() {
  cd ${srcdir}/build-static-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  cd ${srcdir}/build-shared-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYRIGHT \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT
}
